﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Utils;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmKhauTruThueBuoc2 : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private DoiTruThueManagement doitruthueMgt = new DoiTruThueManagement();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();

        private int idLoaiPhieu = 3;
        private int iThangTinhThue = 0;
        private long lTien1331 = 0;
        private long lTien3331 = 0;
        private long lSoKhauTru = 0;
        private long lSoConDuocKhauTru = 0;
        private long lSoNopTrongKy = 0;
        private long lSDDK1331 = 0;
        private long lSDDK3331 = 0;

        public FrmKhauTruThueBuoc2()
        {
            InitializeComponent();
        }

        public FrmKhauTruThueBuoc2(int iThangTinhThue)
        {
            InitializeComponent();
            this.iThangTinhThue = iThangTinhThue;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTiepTheo_Click(object sender, EventArgs e)
        {
            FrmKhauTruThueBuoc3 frm = new FrmKhauTruThueBuoc3(iThangTinhThue, lSoKhauTru, lTien1331, lTien3331, lSDDK1331, lSDDK3331, lSoConDuocKhauTru, lSoNopTrongKy);
            this.Hide();
            frm.ShowDialog();
            this.Close();
        }

        private void btnQuayLai_Click(object sender, EventArgs e)
        {
            FrmKhauTruThueBuoc1 frm = new FrmKhauTruThueBuoc1();
            this.Hide();
            frm.ShowDialog();
            this.Close();
        }

        private void btnKhauTruThue_Click(object sender, EventArgs e)
        {
            ResultObj resultObj = new ResultObj();

            string sDienGiai = "Khấu trừ thuế tháng " + iThangTinhThue.ToString() + " năm " + DateTime.Now.Year.ToString();
            DateTime dtNgayChungTu = new DateTime(DateTime.Now.Year, iThangTinhThue, 1).AddMonths(1).AddDays(-1);
            string sSoChungTu = "DTT/" + dtNgayChungTu.Month.ToString("MM") + dtNgayChungTu.Year.ToString("yy") + "/" + KiemTraMaSo.CheckCode("DTT", idLoaiPhieu, dtNgayChungTu);

            DoiTruThue doitruthue = new DoiTruThue();
            doitruthue.MaSo = sSoChungTu;
            doitruthue.History = "";
            doitruthue.NgayThang = dtNgayChungTu;
            doitruthue.DienGiai = sDienGiai;
            doitruthue.ThueGTGTDauRa3331 = lTien3331;
            doitruthue.ThueGTGTDuocKhauTru1331 = lTien1331;
            doitruthue.TienKhauTru = lSoKhauTru;

            resultObj = doitruthueMgt.SaveNewDoiTruThue(doitruthue);

            if (resultObj.ReturnCode > 0)
            {
                ButToan buttoan = new ButToan();
                buttoan.NgayThang = dtNgayChungTu;
                buttoan.ChiNhanh = "";
                buttoan.MaSoKhachHang = "";
                buttoan.DienGiai = sDienGiai;
                buttoan.SoChungTu = sSoChungTu;
                buttoan.TKGoc = "3331";
                buttoan.TKDoiUng = "1331";
                buttoan.GhiNo = lSoKhauTru;
                buttoan.GhiCo = 0;

                resultObj = buttoanMgt.SaveNewButToan(buttoan);

                buttoan = new ButToan();
                buttoan.NgayThang = dtNgayChungTu;
                buttoan.ChiNhanh = "";
                buttoan.MaSoKhachHang = "";
                buttoan.DienGiai = sDienGiai;
                buttoan.SoChungTu = sSoChungTu;
                buttoan.TKGoc = "1331";
                buttoan.TKDoiUng = "3331";
                buttoan.GhiNo = 0;
                buttoan.GhiCo = lSoKhauTru;

                resultObj = buttoanMgt.SaveNewButToan(buttoan);
            }

            if (resultObj.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                btnTiepTheo.Enabled = false;
                btnQuayLai.Enabled = false;
                btnKhauTruThue.Enabled = false;
                btnIn.Enabled = true;
            }
            else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            string sThoiGian = "Tháng " + iThangTinhThue + " năm " + DateTime.Now.Year.ToString();
            FrmXemReport frmView = new FrmXemReport(sThoiGian, lSDDK1331, lSDDK3331, lTien1331, lTien3331, lSoNopTrongKy, lSoConDuocKhauTru);
            this.Hide();
            frmView.Show();
            this.Show();
        }

        private void FrmKhauTruThueBuoc2_Load(object sender, EventArgs e)
        {
            // Get SoDuBanDau of account 1331
            long lSoDuBanDau1331 = 0;
            TaiKhoanKeToan taikhoanketoan1331 = (from a in GetAllTaiKhoanKeToan()
                                             where a.MaSoTK == "1331"
                                             select a).FirstOrDefault();
            if (taikhoanketoan1331 != null)
                lSoDuBanDau1331 = taikhoanketoan1331.SoDuBanDau;

            // Get SoDuBanDau of account 1331
            long lSoDuBanDau3331 = 0;
            TaiKhoanKeToan taikhoanketoan3331 = (from a in GetAllTaiKhoanKeToan()
                                             where a.MaSoTK == "3331"
                                             select a).FirstOrDefault();

            if (taikhoanketoan3331 != null)
                lSoDuBanDau3331 = taikhoanketoan3331.SoDuBanDau;

            // Get all ButToans of account 1331 from the begining to the end of applied month
            var lst1331 = from c in GetAllButToan()
                      where c.NgayThang.Month <= iThangTinhThue && c.NgayThang.Year == DateTime.Now.Year && c.TKGoc == "1331"
                      select c;

            // Get all ButToans of account 3331 from the begining to the end of applied month
            var lst3331 = from c in GetAllButToan()
                          where c.NgayThang.Month <= iThangTinhThue && c.NgayThang.Year == DateTime.Now.Year && c.TKGoc == "3331"
                          select c;

            // Sum generated of account 1331
            long lPSNo = 0;
            long lPSCo = 0;

            List<ButToan> list1331 = lst1331.ToList();
            for (int i = 0; i < list1331.Count; i++)
            {
                lPSNo += list1331[i].GhiNo;
                lPSCo += list1331[i].GhiCo;
            }
            lTien1331 = (lSoDuBanDau1331 + lPSNo) - lPSCo;

            // Sum generated of account 3331
            lPSNo = 0;
            lPSCo = 0;

            List<ButToan> list3331 = lst3331.ToList();
            for (int i = 0; i < list3331.Count; i++)
            {
                lPSNo += list3331[i].GhiNo;
                lPSCo += list3331[i].GhiCo;
            }
            lTien3331 = (lSoDuBanDau3331 + lPSCo) - lPSNo;


            // Get all ButToans of account 1331 lower than applied month
            lst1331 = lst1331.Where(c => c.NgayThang.Month == iThangTinhThue);

            // Get all ButToans of account 3331 lower than applied month
            lst3331 = lst3331.Where(c => c.NgayThang.Month == iThangTinhThue);

            // Sum generated of account 1331
            lPSNo = 0;
            lPSCo = 0;

            list1331 = lst1331.ToList();
            for (int i = 0; i < list1331.Count; i++)
            {
                lPSNo += list1331[i].GhiNo;
                lPSCo += list1331[i].GhiCo;
            }
            lSDDK1331 = (lTien1331 + lPSCo) - lPSNo;

            // Sum generated of account 3331
            lPSNo = 0;
            lPSCo = 0;

            list3331 = lst3331.ToList();
            for (int i = 0; i < list3331.Count; i++)
            {
                lPSNo += list3331[i].GhiNo;
                lPSCo += list3331[i].GhiCo;
            }
            lSDDK3331 = (lTien3331 + lPSNo) - lPSCo;


            txtThueDKT.Text = lTien1331.ToString();
            txtThueDR.Text = lTien3331.ToString();

            if (lTien1331 > 0 && lTien3331 > 0)
            {
                if (lTien1331 > lTien3331)
                {
                    lSoKhauTru = lTien3331;
                    lSoNopTrongKy = lTien1331 - lTien3331;
                }
                else if (lTien1331 < lTien3331)
                {
                    lSoKhauTru = lTien1331;
                    lSoNopTrongKy = lTien3331 - lTien1331;
                }
            }

            if (lSoKhauTru > 0)
            {
                DataGridViewRow row = new DataGridViewRow();

                DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                cell.Value = lTien3331 > lTien1331 ? "1331" : "3331";
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lTien3331 > lTien1331 ? "3331" : "1331";
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = lSoKhauTru;
                row.Cells.Add(cell);

                cell = new DataGridViewTextBoxCell();
                cell.Value = "Khấu trừ thuế GTGT của hàng hóa, dịch vụ";
                row.Cells.Add(cell);

                dgvChiTiet.Rows.Add(row);
            }
            else
            {
                btnIn.Enabled = false;
                btnTiepTheo.Enabled = false;
                btnKhauTruThue.Enabled = false;
            }
        }

        private void dgvChiTiet_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<DoiTruThue> GetAllDoiTruThue()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = doitruthueMgt.GetAllDoiTruThue();
            List<DoiTruThue> listDoiTruThue = (List<DoiTruThue>)resultObj.Result;

            return listDoiTruThue;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }
    }
}
